package com.fowo.api.service.handleWork;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import com.fowo.api.entity.Product;
import com.fowo.api.mapper.ProductMapper;
import com.fowo.api.model.requisition.RequisitionConfirmReceiptAction;
import com.fowo.api.model.requisition.RequisitionVo;
import com.fowo.api.model.requisition.product.RequisitionProductItemVo;
import com.fowo.api.model.stock.in.StockInVo;
import com.fowo.api.model.stock.in.product.StockInProductItemVo;
import com.fowo.api.service.RequisitionService;
import com.fowo.api.service.StockInService;
import com.fowo.api.service.StockOutService;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 调拨单的确认收货行为接口
 */
@Component
public class RequisitionConfirmReceipt implements RequisitionConfirmReceiptAction {
    @Resource
    private RequisitionService requisitionService;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private StockInService stockInService;
    @Resource
    private StockOutService stockOutService;

    @Override
    public void run(Long id) throws Exception {
        RequisitionVo requisition = requisitionService.getVoById(id);
        Assert.notNull(requisition, "调拨单不存在");

        requisition.setStatus("3");
        createStockIn(requisition);
        requisitionService.goodIn(id);
        requisitionService.badIn(id);
        requisitionService.update(requisition);
    }

    /**
     * 添加入库单
     *
     * @param requisition
     * @throws Exception
     */
    public void createStockIn(RequisitionVo requisition) throws Exception {
        StockInVo stockInVo = new StockInVo();
        stockInVo.setWarehouse(requisition.getToWid());
        stockInVo.setReceiptType("RK005");
        stockInVo.setRemarks("调拨单号：" + requisition.getRqtSn());
        stockInVo.setCostAllocationType("1");
        stockInVo.setWarehousingDate(new Date());
        stockInVo.setPurchaseOrderCode(requisition.getRqtSn());
        stockInVo.setStatus("1");
        List<StockInProductItemVo> productList = new ArrayList<>();
        for (RequisitionProductItemVo item : requisition.getRequisitionProduct()) {
            Product product = productMapper.selectById(item.getProductId());
            Assert.notNull(product,"商品不存在，请检查");
            StockInProductItemVo productItemVo = new StockInProductItemVo();
            productItemVo.setImg(item.getImg());
            productItemVo.setProductId(item.getProductId());
            productItemVo.setSku(item.getSku());
            productItemVo.setUnitStock(Convert.toInt(NumberUtil.add(item.getAvailbleRqtNum(), item.getDefectiveRqtQuantity()), 0));
            productItemVo.setQuantity(item.getAvailbleRqtNum());
//            productItemVo.setGoodProduct(item.getAvailbleRqtNum());
            productItemVo.setPositions(item.getToGoodWarehouseBin());
            productItemVo.setDefectiveProduct(item.getDefectiveRqtQuantity());
            productItemVo.setDefectiveProductPosition(item.getToBadWarehouseBin());
            BigDecimal procurementCost = product.getProcurementCost() == null ? new BigDecimal(0.00) : product.getProcurementCost();
            productItemVo.setUnitPrice(procurementCost);
            productItemVo.setUnitReceiptCost(procurementCost);
            productItemVo.setSubtotal(NumberUtil.mul(item.getAvailbleRqtNum(),procurementCost));
            productItemVo.setShopId(item.getShopName());
            productItemVo.setLxFnsku(item.getFnsku());
            productList.add(productItemVo);
        }
        stockInVo.setProductList(productList);

        stockInService.create(stockInVo);
    }
}
